package com.forever7776.life.web.controller.life.front;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.core.common.enums.TweetStatusEnum;
import com.forever7776.life.core.common.vo.UserBaseInfoVO;
import com.forever7776.life.core.common.vo.UserCommentVO;
import com.forever7776.life.core.common.vo.UserLastBlogVO;
import com.forever7776.life.core.entity.Tweets;
import com.forever7776.life.core.entity.blog.BlogType;
import com.forever7776.life.core.entity.stat.StatTweet;
import com.forever7776.life.core.entity.sys.SysUser;
import com.forever7776.life.core.service.ITweetsService;
import com.forever7776.life.core.service.blog.IBlogService;
import com.forever7776.life.core.service.blog.IBlogTypeService;
import com.forever7776.life.core.service.comment.ICommentTweetService;
import com.forever7776.life.core.service.praise.IPraiseDetailTweetService;
import com.forever7776.life.core.service.stat.IStatTweetService;
import com.forever7776.life.core.service.sys.ISysUserService;
import com.forever7776.life.remote.common.enums.StatusEnum;
import com.forever7776.life.remote.common.vo.ResultVO;
import com.forever7776.life.web.controller.BaseController;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 首页动弹列表
 * </p>
 *
 * @author kz
 * @date 2018-03-28
 */
@Controller
@RequestMapping("/tweet")
public class FrontTweetsController extends BaseController {

    @Autowired
    private ITweetsService iTweetsService;
    @Autowired
    private ICommentTweetService iCommentTweetService;
    @Autowired
    private IStatTweetService iStatTweetService;
    @Autowired
    private IBlogTypeService iBlogTypeService;
    @Autowired
    private IBlogService iBlogService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IPraiseDetailTweetService iPraiseDetailTweetService;

    /**
     * 查看某个动弹
     *
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/{id}")
    public String index(@PathVariable Integer id, ModelMap map) {
        Tweets tweets = iTweetsService.selectById(id);
        String userId = tweets.getUserId();
        List<Integer> thirdIds = new LinkedList<>();
        thirdIds.add(id);
        List<StatTweet> stat = iStatTweetService.selectByTweetIds(thirdIds);
        List<BlogType> list = iBlogTypeService.selectAll(StatusEnum.NORMAL.getKey());
        UserLastBlogVO lastBlog = iBlogService.selectUserLastBlog(userId, id);
        SysUser user = iSysUserService.selectById(userId);
        SysUser currentUser = getSysUser();
        if (currentUser != null) {
            map.put("praiseDetail", iPraiseDetailTweetService.selectPraiseOneByUserIdAndTweetId(currentUser.getId(), id));
        }
        map.put("tweet", tweets);
        map.put("blogCount", iBlogService.selectCount(userId));
        map.put("blogTypes", iBlogTypeService.createBlogTypeMap(list));
        map.put("user", user);
        map.put("lastBlog", lastBlog);
        map.put("stat", CollectionUtils.isNotEmpty(stat) ? stat.get(0) : null);
        map.put("id", id);
        return FRONT_VIEW_PAGE_PATH_LIFES + "front/comment_tweet";
    }


    /**
     * 该动弹评论列表
     *
     * @param id
     * @return
     */
    @RequestMapping("/comments/{id}")
    @ResponseBody
    public JSONObject comments(@PathVariable Integer id) {
        JSONObject result = new JSONObject();
        Tweets tweets = iTweetsService.selectById(id);
        String userId = tweets.getUserId();
        Page<UserCommentVO> page = iCommentTweetService.selectUserCommentsPage(userId, id);
        List<String> userIds = iCommentTweetService.selectUserIdsById(id);
        List<UserBaseInfoVO> users = iSysUserService.selectUserBaseInfos(userIds);
        result.put("page", page);
        result.put("usersMap", createUserMap(users));
        return result;
    }

    /**
     * 首页动弹列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject list() {
        JSONObject result = new JSONObject();
        List<Integer> status = new LinkedList<>();
        status.add(TweetStatusEnum.STATUS.LOCK.getCode());
        String userId = "";
        if (getSysUser() != null) {
            userId = getUserId();
        }

        result.put("page", iTweetsService.selectShowList(userId));
        return result;
    }

    /**
     * 添加动弹阅读数量
     *
     * @param tweetId
     */
    @RequestMapping(value = "/add/read/{tweetId}", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO addRead(@PathVariable Integer tweetId) {
        logger.info("动弹阅读,userId={},tweetId={}", tweetId);
        return iStatTweetService.addReadStat(tweetId);
    }

    /**
     * 给动弹点赞
     *
     * @param tweetId
     */
    @RequestMapping(value = "/add/praise/{tweetId}", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO addPraise(@PathVariable Integer tweetId) {
        String userId = getUserId();
        logger.info("动弹点赞,userId={},tweetId={}", userId, tweetId);
        return iStatTweetService.addPraiseStat(userId, tweetId);
    }

    /**
     * 给动弹取消点赞
     *
     * @param tweetId
     */
    @RequestMapping(value = "/cut/praise/{tweetId}", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO cutPraise(@PathVariable Integer tweetId) {
        String userId = getUserId();
        logger.info("动弹取消点赞,userId={},tweetId={}", userId, tweetId);
        return iStatTweetService.cutPraiseStat(userId, tweetId);
    }


}
